package com.judge.pe;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.judge.pe.bean.PE;
import com.judge.pe.utils.PullPEParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity implements View.OnClickListener {
    private ImageView iv_add;
    private ListView lv_pe;
    private List<PE> pes = new ArrayList<PE>();
    private PeAdapter adapter;
    private PullPEParser pullPEParser;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_add = (ImageView)findViewById(R.id.iv_add);
        iv_add.setOnClickListener(this);
        lv_pe = (ListView)findViewById(R.id.lv_pe);
        //从xml文件中获取数据
        try {
            //System.out.println(MainActivity.class.getResource("/"));
            InputStream is = getAssets().open("/src/assets/pes.xml");
            pullPEParser = new PullPEParser();  //创建PullPEParser实例
            pes = pullPEParser.parse(is);  //解析输入流
            System.out.println(is.toString());
        } catch (Exception e){
            e.printStackTrace();
        }
        Toast.makeText(this,pes.size()+"",Toast.LENGTH_LONG).show();
        adapter = new PeAdapter();
        lv_pe.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_add:

                break;
        }
    }

    private class PeAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return pes.size();
        }

        @Override
        public Object getItem(int position) {
            return pes.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null){
                view = getLayoutInflater().inflate(
                        R.layout.item_pe, null);
                holder = new ViewHolder();
                holder.tv_pname = (TextView) findViewById(R.id.tv_pname);
                holder.tv_time = (TextView) findViewById(R.id.tv_time);
                holder.tv_tools = (TextView) findViewById(R.id.tv_tools);
                holder.tv_introduction = (TextView) findViewById(R.id.tv_introduction);
                holder.tv_summary = (TextView) findViewById(R.id.tv_summary);
                view.setTag(holder);
            }else{
                holder = (ViewHolder) view.getTag();
            }
            PE pe = pes.get(position);
            holder.tv_pname.setText(pe.pname);
            holder.tv_time.setText(pe.time);
            holder.tv_tools.setText(pe.tools);
            holder.tv_introduction.setText(pe.introduction);
            holder.tv_summary.setText(pe.summary);
            return view;
        }
        private  class ViewHolder{
            TextView tv_pname;
            TextView tv_time;
            TextView tv_tools;
            TextView tv_introduction;
            TextView tv_summary;
        }
    }
}
